# -*-coding:utf-8 -*-
from database.entity.wafer import Wafer
from database.entity.chip import Chip
import datetime


def insert_chip(chip_name, wafer_name, shot_position, shot_number, chip_number, polarization_type,
                measurement_parameter, measurement_data, data_unit, data_type):
    date = datetime.datetime.now()
    Chip(chip_name, wafer_name, shot_position, shot_number, chip_number, polarization_type,
         measurement_parameter, measurement_data, data_unit, data_type, date).add()


def insert_wafer(wafer_id, wafer_die_count, wafer_subdie_count, lot_id, batch_number, temperature, humidity):
    Wafer(wafer_id, wafer_die_count, wafer_subdie_count, lot_id, batch_number, temperature, humidity).add()


def query_wafer(**kwargs):
    para_list = []
    para_dict = kwargs
    para_keys = para_dict.keys()
    if "wafer_name" in para_keys:
        para_list += [Wafer.wafer_name == para_dict["wafer_name"]]
    return Wafer.query(Wafer, filter=para_list)





def query_chip(**kwargs):
    para_list = []
    para_dict = kwargs
    para_keys = para_dict.keys()
    if "data_type" in para_keys:
        para_list += [Chip.data_type == para_dict["data_type"]]
    elif "chip_name" in para_keys:
        para_list += [Chip.chip_name == para_dict["chip_name"]]
    elif "wafer_name" in para_keys:
        para_list += [Chip.wafer_name == para_dict["wafer_name"]]
    elif "measurement_parameter" in para_keys:
        para_list += [Chip.measurement_parameter == para_dict["measurement_parameter"]]
    elif "shot_position" in para_keys:
        para_list += [Chip.shot_position == para_dict["shot_position"]]
    return Chip.query(Chip, filter=para_list)


def delete_chip(chip_name):
    Chip.delete([Chip.chip_name == chip_name])


def delete_chips(chip_names):
    for chip_name in chip_names:
        delete_chip(chip_name)


def delete_wafer(wafer_name):
    Chip.delete([Chip.wafer_name == wafer_name])
    Wafer.delete([Wafer.wafer_name == wafer_name])


def delete_wafers(wafer_names):
    for wafer_name in wafer_names:
        delete_wafer(wafer_name)


kwargs = {"data_type": "Spot"}
result = query_chip(**kwargs)
print(type(result),len(result))
